package com.xh.bussiness.teachbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPlanDetailDto;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPlanDto;
import com.xh.bussiness.teachbook.bean.request.QueryTeachingBookPlanRequest;
import com.xh.bussiness.teachbook.dao.TeachingBookPlanBindMapper;
import com.xh.bussiness.teachbook.dao.TeachingBookPlanDetailMapper;
import com.xh.bussiness.teachbook.dao.TeachingBookPlanMapper;
import com.xh.bussiness.teachbook.model.TeachingBook;
import com.xh.bussiness.teachbook.model.TeachingBookPlan;
import com.xh.bussiness.teachbook.model.TeachingBookPlanBind;
import com.xh.bussiness.teachbook.model.TeachingBookPlanDetail;
import com.xh.bussiness.teachbook.service.ITeachingBookPlanDetailService;
import com.xh.bussiness.teachbook.service.ITeachingBookPlanService;
import com.xh.bussiness.teachbook.service.ITeachingBookService;
import com.xh.bussiness.thirdapi.LongwenApi;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class TeachingBookPlanServiceImpl extends BaseServiceImpl<TeachingBookPlanMapper, TeachingBookPlan> implements ITeachingBookPlanService {

    @Autowired
    private ITeachingBookPlanDetailService teachingBookPlanDetailService;
    @Autowired
    private TeachingBookPlanBindMapper teachingBookPlanBindMapper;
    @Autowired
    private TeachingBookPlanDetailMapper teachingBookPlanDetailMapper;
    @Autowired
    private ITeachingBookService teachingBookService;
    @Override
    public TeachingBookPlanDto queryTeachingBookPlan(QueryTeachingBookPlanRequest request) {
        QueryWrapper<TeachingBookPlan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.eq("ref_id", request.getRefId());
        //如果为一对一
        if (request.getRefType().equals(1)) {
            if (StringUtils.isEmpty(request.getContractId())) {
                return new TeachingBookPlanDto();
            }
            planQueryWrapper.eq("contract_id", request.getContractId());
        }
        TeachingBookPlan teachingBookPlan = this.selectFirst(planQueryWrapper);
        if (teachingBookPlan == null) {
            TeachingBookPlanDto teachingBookPlanDto = new TeachingBookPlanDto();
            teachingBookPlanDto.setTeachingBookPlanDetailDtos(new ArrayList<>());
            return teachingBookPlanDto;
        }
        TeachingBookPlanDto teachingBookPlanDto = new TeachingBookPlanDto();
        BeanUtils.copyProperties(teachingBookPlan, teachingBookPlanDto);
        QueryWrapper<TeachingBookPlanDetail> detailQueryWrapper = new QueryWrapper<>();
        detailQueryWrapper.eq("teaching_book_plan_id", teachingBookPlanDto.getId());
        detailQueryWrapper.orderByAsc("create_time");
        List<TeachingBookPlanDetailDto> teachingBookPlanDetailDtos = teachingBookPlanDetailService.selectList(detailQueryWrapper, TeachingBookPlanDetailDto.class);

        List<TeachingBookPlanDetailDto> rootList = teachingBookPlanDetailDtos.stream().filter(item -> item.getParentId().equals("0")).collect(Collectors.toList());
        for (TeachingBookPlanDetailDto teachingBookPlanDetailDto : rootList) {
            //计划详情子节点数据
            List<TeachingBookPlanDetailDto> childrenList = teachingBookPlanDetailDtos.stream().filter(item -> item.getParentId().equals(teachingBookPlanDetailDto.getId())).sorted((item1, item2) -> item1.getOrderNo() - item2.getOrderNo()).collect(Collectors.toList());
            for (int i = 0; i < childrenList.size(); i++) {
                TeachingBookPlanDetailDto item = childrenList.get(i);
                QueryWrapper<TeachingBookPlanBind> bindQueryWrapper = new QueryWrapper<>();
                bindQueryWrapper.eq("plan_detail_id", item.getId());
                TeachingBookPlanBind teachingBookPlanBind = teachingBookPlanBindMapper.selectOne(bindQueryWrapper);
                if (teachingBookPlanBind != null) {
                    item.setClassTimeStr(teachingBookPlanBind.getClassTimeStr());
                    //查询备课状态
                    if (!StringUtils.isEmpty(teachingBookPlanBind.getTeachingBookId())) {
                        TeachingBook teachingBook = teachingBookService.getById(teachingBookPlanBind.getTeachingBookId());
                        if (teachingBook != null) {
                            item.setAuditStatus(teachingBook.getAuditStatus());
                            item.setTeachingBookId(teachingBook.getId());
                            item.setTeachingBookType(teachingBook.getTeachingBookType());
                        }
                    }
                }
            }
            teachingBookPlanDetailDto.setChildList(childrenList);
            //根节点总共课时
            if (!CollectionUtils.isEmpty(childrenList)) {
                Integer sumClassHour = childrenList.stream().collect(Collectors.summingInt(item -> item.getClasshour()));
                teachingBookPlanDetailDto.setClasshour(sumClassHour);
            }
        }
        teachingBookPlanDto.setTeachingBookPlanDetailDtos(rootList);
        return teachingBookPlanDto;
    }

    @Override
    public String deleteTextbookPlanDetail(String detailPlanId) {
        TeachingBookPlanDetail teachingBookPlanDetail = teachingBookPlanDetailMapper.selectById(detailPlanId);
        if (teachingBookPlanDetail == null) {
            return "课时安排不存在！";
        }
        //是否包含子节点
        QueryWrapper qw = new QueryWrapper();
        qw.eq("parent_id", "planId");
        List<TeachingBookPlanDetail> list = teachingBookPlanDetailMapper.selectList(qw);
        if (!CollectionUtils.isEmpty(list)) {
            return "该阶段下已包含课时安排，删除失败！";
        }
        QueryWrapper bindWrapper = new QueryWrapper();
        bindWrapper.eq("plan_detail_id", detailPlanId);
        TeachingBookPlanBind teachingBookPlanBind = teachingBookPlanBindMapper.selectOne(bindWrapper);
        if (teachingBookPlanBind != null) {
            return "课时安排已添加到上课 " + (StringUtils.isEmpty(teachingBookPlanBind.getClassTimeStr()) ? "" : teachingBookPlanBind.getClassTimeStr());
        }
        teachingBookPlanDetailMapper.deleteById(teachingBookPlanDetail.getId());
        return null;
    }

    @Override
    public List<TeachingBookPlan> queryTeachingBookPlanIdsByLwStudentId(String lwStudentId) {
        if (StringUtils.isEmpty(lwStudentId)) {
            return new ArrayList<>();
        }
        List<TeachingBookPlan> all = new ArrayList<>();
        //根据龙文学生id查询教学计划
        QueryWrapper onebyoneQw = new QueryWrapper();
        onebyoneQw.eq("ref_id", lwStudentId);
        onebyoneQw.eq("type", 1);
        List<TeachingBookPlan> onebyonePlans = this.dao.selectList(onebyoneQw);
        if (!CollectionUtils.isEmpty(onebyonePlans)) {
            all.addAll(onebyonePlans);
        }
        //查询小班教学计划
        List<String> classIds = LongwenApi.queryClassIdsByStuId(lwStudentId);
        if (!CollectionUtils.isEmpty(classIds)) {
            QueryWrapper clsQw = new QueryWrapper();
            clsQw.in("ref_id", classIds);
            clsQw.eq("type", 7);
            List<TeachingBookPlan> clsPlans = this.dao.selectList(clsQw);
            if (!CollectionUtils.isEmpty(clsPlans)) {
                all.addAll(clsPlans);
            }
        }
        if (CollectionUtils.isEmpty(all)) {
            return new ArrayList<>();
        }
        all = all.stream().sorted(new Comparator<TeachingBookPlan>() {
            @Override
            public int compare(TeachingBookPlan o1, TeachingBookPlan o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        }).collect(Collectors.toList());
        return all;
    }
}
